package cn.rick.controller;

import cn.dev33.satoken.util.SaResult;
import cn.rick.entity.CustomerSea;
import cn.rick.service.ICustomerSeaService;
import cn.rick.util.EasyExcelUtil;
import cn.rick.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 客户海 前端控制器
 * </p>
 *
 * @author house_store
 * @since 2022-10-29 10:40:00
 */
@RestController
@RequestMapping("/api/customerSea")
@Api(value = "客户海", tags = {"客户海"})
public class CustomerSeaController {

    @Resource
    private ICustomerSeaService iCustomerSeaService;

    @GetMapping("/getData")
    @ApiOperation("获取所有客户信息")
    public SaResult getData(CustomerSea customerSea, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<CustomerSea> query = new LambdaQueryWrapper<>();
        query.orderByDesc(CustomerSea::getCreateTime);

        if (StringUtils.isNotEmpty(customerSea.getName())) {
            query.like(CustomerSea::getName, customerSea.getName());
        }
        if (StringUtils.isNotEmpty(customerSea.getDirector())) {
            query.like(CustomerSea::getDirector, customerSea.getDirector());
        }
        if (StringUtils.isNotEmpty(customerSea.getSource())) {
            query.eq(CustomerSea::getSource, customerSea.getSource());
        }
        if (StringUtils.isNotEmpty(customerSea.getNature())) {
            query.eq(CustomerSea::getNature, customerSea.getNature());
        }
        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            query.between(CustomerSea::getCreateTime, startTime, endTime + " 23:59:59");
        }

        PageHelper.startPage(pageNum, pageSize);
        List<CustomerSea> list = iCustomerSeaService.list(query);
        PageInfo<CustomerSea> pageInfo = new PageInfo<>(list);
        return SaResult.data(pageInfo);
    }

    @GetMapping("/download")
    @ApiOperation("获取所有客户信息")
    public void download(CustomerSea customerSea, String startTime, String endTime, HttpServletResponse response) {
        LambdaQueryWrapper<CustomerSea> query = new LambdaQueryWrapper<>();
        query.orderByDesc(CustomerSea::getCreateTime);

        if (StringUtils.isNotEmpty(customerSea.getName())) {
            query.like(CustomerSea::getName, customerSea.getName());
        }
        if (StringUtils.isNotEmpty(customerSea.getDirector())) {
            query.like(CustomerSea::getDirector, customerSea.getDirector());
        }
        if (StringUtils.isNotEmpty(customerSea.getSource())) {
            query.eq(CustomerSea::getSource, customerSea.getSource());
        }
        if (StringUtils.isNotEmpty(customerSea.getNature())) {
            query.eq(CustomerSea::getNature, customerSea.getNature());

        }
        if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            query.between(CustomerSea::getCreateTime, startTime, endTime + " 23:59:59");
        }

        List<CustomerSea> list = iCustomerSeaService.list(query);
        EasyExcelUtil.writeExcel(response, list, CustomerSea.class, "客户信息");
    }

    @PostMapping("/save")
    @ApiOperation("保存客户信息")
    public SaResult save(@RequestBody CustomerSea customerSea) {
        if (StringUtils.isNotNull(customerSea.getId())) {
            customerSea.setUpdateTime(LocalDateTime.now());
        }
        if (iCustomerSeaService.saveOrUpdate(customerSea)) {
            return SaResult.ok("保存成功");
        } else {
            return SaResult.error("保存失败，请稍后再试");
        }
    }

}
